﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TravelSite.Entities;
using Dapper;

namespace TravelSite.DAL.Repositories
{
    public class ReservationRepository : BaseRepository<Reservation>
    {
        protected override string TableName
        {
            get { return "[Reservation]"; }
        }

        public override IEnumerable<Reservation> FindAll()
        {
            string strSQL = @"  select vo.ReservationId, vo.ReservationName,
                    convert(bit, case 
                                    when 1 = 0 then 0
                                else 1 end) as CanDelete
                    from [Reservation] (nolock) as vo 
                    order by vo.ReservationName ";

            IEnumerable<Reservation> result = Connection.Query<Reservation>(strSQL);
            return result;
        }

        public override Reservation Find(int id)
        {
            Reservation result = Connection.Query<Reservation>(
                @"  select vo.ReservationId, vo.ReservationName
                    from [Reservation] (nolock) as vo
                    where vo.ReservationId = @ReservationId", new { ReservationId = id }).SingleOrDefault();
            return result;
        }

        public override void Insert(Reservation item)
        {
            Connection.Execute(
                @"insert into [Reservation] 
                (ReservationName)
                values (@ReservationName)", item);
        }

        public override void Update(Reservation item)
        {
            Connection.Execute(
                @"update Reservation
                    set ReservationName = @ReservationName
                    where (ReservationId = @ReservationId)", item);
        }

        public override void Delete(int id)
        {
            Connection.Execute(
                @"delete 
                    from Reservation
                    where (ReservationId = @ReservationId)", new { ReservationId = id });
        }

    }
}
